package com.bosssoft.goods.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.goods.domain.bean.GoodsBrand;
import com.bosssoft.goods.domain.vo.AddBrandVO;
import com.bosssoft.goods.domain.vo.EditBrandVO;
import com.bosssoft.goods.domain.vo.SetCategoryBrandVO;
import com.bosssoft.goods.service.GoodsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品品牌API接口
 * @author Fang_Sir
 *@date 2021-04-13
 */
@RestController
@RequestMapping("/manage/brand")
@Api(tags = "2-商品品牌管理")
public class GoodsBrandController {
    @Autowired
    private GoodsBrandService goodsBrandService;

    @GetMapping("/getBrandList")
    @ApiOperation(value = "获取品牌列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "页大小", defaultValue = "10",dataType = "int"),
    })
    public Result<?> getBrandList(@RequestParam(required = false,defaultValue = "1") int page,
                              @RequestParam(required = false,defaultValue = "10")int size){
        return Result.ok(goodsBrandService.getBrandList(page, size));
    }

    @PostMapping("/addBrand")
    @ApiOperation(value = "添加品牌")
    public Result<?> addBrand(@Valid @RequestBody AddBrandVO brand, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        if(goodsBrandService.addBrand(brand.getName())) {
            return Result.ok();
        }
        return Result.error();
    }

    @GetMapping("/isExistBrand")
    @ApiOperation(value = "判断品牌是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "品牌名", dataType = "String")
    })
    public Result<?> isExistBrand(String name){
        return Result.ok(goodsBrandService.isExistBrand(name));
    }

    @GetMapping("/getBrand")
    @ApiOperation(value = "获取品牌信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "品牌名", dataType = "String")
    })
    public Result<?> getBrand(String name){
        GoodsBrand brand = goodsBrandService.getBrandInfo(name);
        if(brand != null) {
            return Result.ok(goodsBrandService.getBrandInfo(name));
        } else {
            return Result.error().message("品牌不存在或已删除！");
        }
    }

    @PutMapping("/editBrand")
    @ApiOperation(value = "编辑品牌信息")
    public Result<?> editBrand(@Valid @RequestBody EditBrandVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            if (goodsBrandService.editBrand(info)) {
                return Result.ok();
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.error();
    }

    @DeleteMapping("/delBrand/{brandId}")
    @ApiOperation(value = "删除品牌信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌编号", dataType = "int",paramType = "path")
    })
    public Result<?> delBrand(@PathVariable("brandId") int brandId){
        if(goodsBrandService.delBrand(brandId)){
            return Result.ok();
        }
        return Result.error();
    }

    @PostMapping("/bindCatBrand")
    @ApiOperation(value = "关联三级分类与品牌")
    public Result<?> bindCategoryBrand(@Valid @RequestBody SetCategoryBrandVO catBrand, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            if (goodsBrandService.bindCatBrand(catBrand.getCatId(), catBrand.getBrandId())) {
                return Result.ok();
            }
        } catch (Exception e) {
            return Result.error("商品分类或品牌不存在");
        }
        return Result.error("添加失败！商品已添加！");
    }

    @GetMapping("/getCatBrands/{catId}")
    @ApiOperation(value = "获取相关第三级分类的所有品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catId", value = "查询的三级分类编号", required = true, dataType = "int",paramType = "path")
    })
    public Result<?> setCategoryBrand(@PathVariable("catId") int catId){
        return Result.ok(goodsBrandService.getCatBrandList(catId));
    }

    @DeleteMapping("/rmCatBrands/{catId}/{brandId}")
    @ApiOperation(value = "取消三级分类与品牌的关联")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catId", value = "三级分类编号", required = true, dataType = "int",paramType = "path"),
            @ApiImplicitParam(name = "brandId", value = "品牌编号", required = true, dataType = "int",paramType = "path")
    })
    public Result<?> removeCategoryBrand(@PathVariable("catId") int catId,@PathVariable("brandId") int brandId){
        return Result.ok(goodsBrandService.removeCategoryBrand(catId, brandId));
    }
}
